package com.bjdd.jdy.ui.bankCard;

import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.T;

/**
 * Created by Administrator on 2017/5/10.
 */

public class BankCardPresenter extends BankCardContract.Presenter {
    @Override
    public void onStart() {
        if (SpUtil.getUser() == null || SpUtil.getUser().equals("") || SpUtil.getUser().getCarrierId() == null) {
            JumpUtil.toLogin(context);
            return;
        }
        getCard();
    }

    @Override
    void getCard() {
        //showLoadingDialog(false);
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.bankCards(token,carrierId)
        .subscribe(result -> {
            hideLoadingDialog();
            if (!result.isError()){
                view.cardBack(result.getData());
            }else {
                T.show(result.getMsg());
            }
        },e -> {
            hideLoadingDialog();
            T.show("获取失败");
        }));
    }

    @Override
    void delCard(String cardId,int position) {
        showLoadingDialog(false);
        String token = SpUtil.getToken();
        String carrierId = SpUtil.getUser().getCarrierId();
        rxManager.add(model.delCard(token,carrierId,cardId)
                .subscribe(result -> {
                    hideLoadingDialog();
                    if (!result.isError()){
                        view.delCard(position);
                    }else {
                        T.show(result.getMsg());
                    }
                },e -> {
                    hideLoadingDialog();
                    T.show("删除失败");
                }));
    }
}
